package com.zxy.study.base.recyclerview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zxy.study.utils.DimenUtils;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.annotation.StringRes;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @Author : Shonn.zhang
 * @Date :  2020/7/21 10:09
 * @Desc :  封装ViewHolder通用的操作（View初始化，资源设置），简化开发
 */
public class BaseViewHolder extends RecyclerView.ViewHolder {

    private SparseArray<View> mCacheViews = new SparseArray<>();

    public BaseViewHolder(@NonNull View itemView) {
        super(itemView);
    }

    public <T extends View> T getView(@IdRes int viewId) {
        T t = (T) mCacheViews.get(viewId);
        if (t == null) {
            t = itemView.findViewById(viewId);
            if (t != null) {
                mCacheViews.put(viewId, t);
            }
        }
        return t;
    }

    public Context getContext() {
        return itemView.getContext();
    }

    protected int getShowWidth() {
        return ViewGroup.LayoutParams.WRAP_CONTENT;
    }

    protected int getShowHeight() {
        return ViewGroup.LayoutParams.WRAP_CONTENT;
    }

    public final void show() {
        ViewGroup.LayoutParams lp = itemView.getLayoutParams();
        if (lp != null) {
            lp.width = getShowWidth();
            lp.height = getShowHeight();
            itemView.requestLayout();
        }
    }

    /**
     * 支持给楼层添加分割Margin
     *
     * @param bottom
     */
    public final void show(int bottom) {
        ViewGroup.LayoutParams lp = itemView.getLayoutParams();
        if (lp != null) {
            lp.width = getShowWidth();
            lp.height = getShowHeight();
            if (lp instanceof ViewGroup.MarginLayoutParams) {
                ((ViewGroup.MarginLayoutParams) lp).bottomMargin = DimenUtils.dpToPx(getContext(), bottom);
            }
            itemView.requestLayout();
        }
    }

    public final void hide() {
        ViewGroup.LayoutParams lp = itemView.getLayoutParams();
        if (lp != null) {
            lp.width = 0;
            lp.height = 0;
            if (lp instanceof ViewGroup.MarginLayoutParams) {
                ((ViewGroup.MarginLayoutParams) lp).bottomMargin = 0;
            }
            itemView.requestLayout();
        }
    }


    public BaseViewHolder setText(@IdRes int viewId, CharSequence charSequence) {
        ((TextView) getView(viewId)).setText(charSequence);
        return this;
    }

    public BaseViewHolder setText(@IdRes int viewId, @StringRes int stringRes) {
        ((TextView) getView(viewId)).setText(stringRes);
        return this;
    }

    public BaseViewHolder setTextColor(@IdRes int viewId, @ColorInt int color) {
        ((TextView) getView(viewId)).setTextColor(color);
        return this;
    }

    public BaseViewHolder setTextColorRes(@IdRes int viewId, @ColorRes int color) {
        ((TextView) getView(viewId)).setTextColor(itemView.getResources().getColor(color));
        return this;
    }

    public BaseViewHolder setImageRes(@IdRes int viewId, @DrawableRes int drawableRes) {
        ((ImageView) getView(viewId)).setImageResource(drawableRes);
        return this;
    }

    public BaseViewHolder setImageDrawable(@IdRes int viewId, Drawable drawable) {
        ((ImageView) getView(viewId)).setImageDrawable(drawable);
        return this;
    }

    public BaseViewHolder setImageBitmap(@IdRes int viewId, Bitmap bitmap) {
        ((ImageView) getView(viewId)).setImageBitmap(bitmap);
        return this;
    }

    public BaseViewHolder setBackgroundRes(@IdRes int viewId, @DrawableRes int drawableRes) {
        getView(viewId).setBackgroundResource(drawableRes);
        return this;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public BaseViewHolder setBackgroundDrawable(@IdRes int viewId, Drawable drawable) {
        getView(viewId).setBackground(drawable);
        return this;
    }

    public BaseViewHolder setBackgroundColor(@IdRes int viewId, @ColorInt int color) {
        getView(viewId).setBackgroundColor(color);
        return this;
    }

    public BaseViewHolder setViewGone(@IdRes int viewId, boolean visible) {
        getView(viewId).setVisibility(visible ? View.VISIBLE : View.GONE);
        return this;
    }

    public BaseViewHolder setViewInVisible(@IdRes int viewId, boolean visible) {
        getView(viewId).setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
        return this;
    }

    public BaseViewHolder setViewEnable(@IdRes int viewId, boolean enable) {
        getView(viewId).setEnabled(enable);
        return this;
    }

    public BaseViewHolder setViewSelected(@IdRes int viewId, boolean selected) {
        getView(viewId).setSelected(selected);
        return this;
    }

}
